package com.booksys.controller;

import com.booksys.mapper.ReaderMapper;
import com.booksys.pojo.Reader;
import com.booksys.result.ResultUtil;
import com.booksys.util.DateTimeUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
@CrossOrigin
@Api(tags = "用户相关接口")
public class UserController {

    //TODO
    @Autowired
    private ReaderMapper readerMapper;
    /**
     * 用户登录
     * @param account
     * @param password
     * @return
     */
    @ApiOperation(value = "用户登录")
    @RequestMapping(value = "/login", method={RequestMethod.POST,RequestMethod.GET})
    public ResultUtil<Reader> login(@RequestParam("account")@ApiParam(value="用户名称")String account,
                                    @RequestParam("password")@ApiParam(value="用户密码")String password){
        ResultUtil<Reader> result = new ResultUtil<>();
        Reader reader=readerMapper.selectWholeByAccount(account);
        if(reader!=null){
            if(reader.getPassword().equals(password)){
                return result.success(reader);
            }
        }
        return result.fail(reader);
    }

    /**
     * 用户注册
     * @param account
     * @param name
     * @param password
     * @param sex
     * @param condi
     */
    @ApiOperation(value = "用户注册")
    @RequestMapping(value = "/register", method={RequestMethod.POST,RequestMethod.GET})
    public ResultUtil<Reader> register(@RequestParam("account")@ApiParam(value="用户名称") String account,
                                       @RequestParam("name")@ApiParam(value="用户姓名") String name,
                                       @RequestParam("password")@ApiParam(value="用户密码") String password,
                                       @RequestParam("sex")@ApiParam(value="用户性别") String sex,
                                       @RequestParam("condi")@ApiParam(value="用户类别") int condi){
        ResultUtil<Reader> result = new ResultUtil<>();
        Reader reader = readerMapper.selectWholeByAccount(account);
        if(reader==null) {
            /*插入用户*/
            try {
                readerMapper.insert(new Reader(account, password, name, sex, DateTimeUtil.getDate(), condi));
            } catch (Exception e) {
                e.printStackTrace();
            }
            return result.success(reader);
        }else {
            return result.fail(reader);
        }
    }

    /**
     *权限修改
     * @param account
     * @return
     */
    @ApiOperation(value = "删除用户")
    @RequestMapping(value = "/deleteReaders", method={RequestMethod.POST,RequestMethod.GET})
    public ResultUtil<Reader> updaterole(@RequestParam("account")@ApiParam(value="用户名称") String account){
        ResultUtil<Reader> result = new ResultUtil<>();
        Reader reader=readerMapper.selectWholeByAccount(account);
        if(reader==null){
            //不存在该用户
            return result.fail(reader);
        }
        //获取id，根据id修改权限

        try {
            //修改成功
            readerMapper.deleteByAccount(account);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result.success(reader);
    }

    /**
     * 展示所有用户
     * @param account
     * @param currentPage
     * @return
     */
    @ApiOperation(value = "查询用户")
    @RequestMapping(value = "/getAllReaders", method={RequestMethod.POST,RequestMethod.GET})
    public ResultUtil<List<Reader>> getAllReaders(@RequestParam("account")@ApiParam(value="用户名称")String account,
                                            @RequestParam("currentPage")@ApiParam(value="当前页码")int currentPage){

        ResultUtil<List<Reader>> listResultUtil = new ResultUtil<>();

        List<Reader> list = readerMapper.selectByAccount(account);

        return listResultUtil.success(list);
    }

    /**
     * 添加用户业务
     * @param account
     * @param name
     * @param sex
     * @param condi
     * @return
     */
    @ApiOperation(value = "添加用户")
    @RequestMapping(value = "/addReader", method={RequestMethod.POST,RequestMethod.GET})
    public ResultUtil<Reader> addUser(@RequestParam("account")@ApiParam(value="用户名称") String account,
                                      @RequestParam("name")@ApiParam(value="用户姓名") String name,
                                      @RequestParam("sex")@ApiParam(value="用户性别") String sex,
                                      @RequestParam("condi")@ApiParam(value="用户类别") int condi){
        /*1.声明返回结果*/
        ResultUtil<Reader> result = new ResultUtil<>();
        /*2.判断是否有重复*/
        if(readerMapper.selectByAccount(account).size()!=0){
            /*2.1.重复，返回失败*/
            return result.fail();
       }else{
            //默认密码，可更改密码生成逻辑
            String password="123456";
            //当前注册时间
            String time=DateTimeUtil.getDate();
            Reader reader=new Reader(account,password,name,sex,time,condi);
            readerMapper.insert(reader);
            //返回状态码
            return result.success();
        }
    }
}
